package com.cmcc.speed

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.graphics.BitmapFactory
import android.location.Location
import android.media.MediaScannerConnection
import android.net.Uri
import android.net.wifi.ScanResult
import android.net.wifi.WifiManager
import android.os.*
import android.util.Log
import android.view.View
import android.webkit.*
import android.widget.Toast
import androidx.activity.addCallback
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.net.toFile
import androidx.lifecycle.lifecycleScope
import androidx.work.WorkInfo.State.*
import androidx.work.WorkManager
import com.cmcc.speed.databinding.ActivityHouseBinding
import com.cmcc.speed.http.KCHttpV2
import com.cmcc.speed.utils.*
import com.cmcc.speed.utils.CommonUtils.md5
import com.cmcc.speed.utils.CommonUtils.saveBitmapGallery
import com.cmcc.speed.utils.Constants.DATE_FORMAT
import com.cmcc.speed.utils.Constants.PHOTO_EXTENSION
import com.cmcc.speed.utils.Constants.REQUIRED_PERMISSIONS
import com.cmcc.speed.utils.FileUtils.createFile
import com.cmcc.speed.utils.FileUtils.getOutputDirectory
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXImageObject
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import kotlinx.coroutines.launch
import okhttp3.*
import org.json.JSONObject
import java.io.*
import java.util.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.concurrent.schedule


class HouseActivity : AppCompatActivity() {

    private lateinit var binding: ActivityHouseBinding
    lateinit var activityWebView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityHouseBinding.inflate(layoutInflater)
        setContentView(binding.root)


        activityWebView = binding.toolWebview
        setWebWs()
        activityWebView.loadUrl("https://networkorder.komect.com/reserveH5/pages/headerTest/?province=7219RU")

    }

    @SuppressLint("JavascriptInterface")
    private fun setWebWs() {
        activityWebView.background.alpha = 0; // 设置填充透明度 范围：0-255
        activityWebView.webViewClient = WebViewClient()
        val ws: WebSettings? = activityWebView?.settings
        //这写注释先不要删掉
        //ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//自适应屏幕  //导致4.4以下摇钱树只显示背景和标题
        ws?.javaScriptCanOpenWindowsAutomatically = false;//自动打开窗口
//        ws?.setSupportZoom(true);// 设置支持缩放
        ws?.builtInZoomControls = true;// 设置支持缩放
        ws?.displayZoomControls = false;//显示缩放按
        ws?.allowFileAccess = true;// -> 是否允许访问文件
        ws?.domStorageEnabled = true; //-> 是否节点缓存
        ws?.databaseEnabled = true;// -> 是否数据缓存
        ws?.setAppCacheEnabled(true);// -> 是否应用缓存
        ws?.loadWithOverviewMode = true
        ws?.builtInZoomControls = true
        //? ws.setAllowFileAccessFromFileURLs(false);
        //? ws.setAllowFileAccessFromFileURLs(false);
        ws?.useWideViewPort = true //图片被缩放后显示不全的问题
        ws?.javaScriptEnabled = true
        ws?.savePassword = false
        ws?.blockNetworkImage = false
        ws?.loadsImagesAutomatically = true


        activityWebView?.addJavascriptInterface(this, "local_android");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.0以上默认不允许http和https混合模式, 需手动开启
            ws?.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }

        val backBtn = binding.goBack
        backBtn.setOnClickListener{
            onBack()
        }
    }

    fun onBack() {
        onBackPressed()
    }

}
